//
//  SubmitIssueViewController.m
//  Care With Friends
//
//  Created by Versatile Systems, Inc on 11/4/13.
//  Copyright (c) 2013 Muludiang. All rights reserved.
//

#import "SubmitIssueViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CWFData.h"
@interface SubmitIssueViewController ()

@end

@implementation SubmitIssueViewController
@synthesize contactName;
@synthesize accountPhone;
@synthesize preferredContact;
@synthesize issueTags;
@synthesize issueDescription;
@synthesize issueSummary;


-(IBAction)hideKeyboard:(id)sender{
    [contactName resignFirstResponder];
    [accountPhone resignFirstResponder];
    [preferredContact resignFirstResponder];
    [issueTags resignFirstResponder];
    [issueDescription resignFirstResponder];
    [issueSummary resignFirstResponder];
}

-(IBAction)clearIssue:(id)sender{
    UIAlertView *clearIssueAlert = [[UIAlertView alloc]
                              initWithTitle:@"Clearing Issue" message:@"Are you sure you want to erase this issue?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
    [clearIssueAlert setTag:0];
    [clearIssueAlert show];
}

-(IBAction)submitIssue:(id)sender{
    UIAlertView *submitAlert;
    if( ([[[contactName text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length ] == 0) || ([[[accountPhone text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length ] == 0) || ([[[preferredContact text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length ] == 0) || ([[[issueDescription text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length ] == 0) || ([[[issueSummary text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length ] == 0)){
        submitAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Please fill out all fields" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil ];
        [submitAlert setTag:1];
    } else{
        submitAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Issue Sumbitted!" delegate:self cancelButtonTitle:@"Home" otherButtonTitles:nil ];
        [submitAlert setTag:2];
    }
    [submitAlert show];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *email;
    NSString *mobile;
    NSCharacterSet *notDigits = [[NSCharacterSet decimalDigitCharacterSet]invertedSet];
    if ([preferredContact.text rangeOfCharacterFromSet:notDigits].location == NSNotFound){
        email = @"";
        mobile = preferredContact.text;
    }
    else{
        email = preferredContact.text;
        mobile = @"";
    }
    if (alertView.tag == 2){
        CWFData *dataObj = [CWFData sharedInstance];
        [dataObj createProblem:contactName.text customerPhone:accountPhone.text custEmail:email custMobilePhone:mobile title:[issueSummary text] description:[issueDescription text] tags:[[NSMutableArray alloc] initWithObjects:issueTags.text, nil]];
            [self.navigationController popToRootViewControllerAnimated:YES];

    }
}

-(BOOL)textViewDidBeginEditing:(UITextField *)textField{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.35f];
    CGRect frame = self.view.frame; frame.origin.y = -150;
    [self.view setFrame:frame];
    [UIView commitAnimations];
    return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextField *)textField{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.35f];
    CGRect frame = self.view.frame; frame.origin.y = 0;
    [self.view setFrame:frame];
    [UIView commitAnimations];
    return YES;
}


-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if ((textField.tag == 4) ||(textField.tag == 5)) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.35f];
        CGRect frame = self.view.frame; frame.origin.y = -150;
        [self.view setFrame:frame];
        [UIView commitAnimations];
    }
    else{
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.35f];
        CGRect frame = self.view.frame; frame.origin.y = 0;
        [self.view setFrame:frame];
        [UIView commitAnimations];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.35f];
    CGRect frame = self.view.frame; frame.origin.y = 0;
    [self.view setFrame:frame];
    [UIView commitAnimations];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [[self.issueDescription layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
    [[self.issueDescription layer] setBorderWidth:1.5];
    [[self.issueDescription layer] setCornerRadius:5];
    
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    [toolbar setBarStyle:UIBarStyleBlackTranslucent];
    [toolbar sizeToFit];
    
    UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard:)];
    
    NSArray *itemsArray = [NSArray arrayWithObjects:flexButton, doneButton, nil];
    
    [toolbar setItems:itemsArray];
    
    [contactName setInputAccessoryView:toolbar];
    [accountPhone setInputAccessoryView:toolbar];
    [preferredContact setInputAccessoryView:toolbar];
    [issueTags setInputAccessoryView:toolbar];
    [issueSummary setInputAccessoryView:toolbar];
    [issueDescription setInputAccessoryView:toolbar];
    
    [super viewDidLoad];
	// Do any additional setup after loading theview.
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
